package daoimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pojo.Roles;
import pojo.UserProfile;
import dao.RoleDAO;
import dao.UserDAO;

@Repository("UserDAO")
public class UserDAOImpl implements UserDAO {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Autowired
	private RoleDAO RoleDAO;

	public UserProfile getUserProfile(String email) {
		UserProfile user = null;

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		try {
			String hql = "Select u from UserProfile u where u.email=:email";
			Query query = session.createQuery(hql);
			query.setParameter("email", email);
			user = (UserProfile) query.uniqueResult();
		} catch (HibernateException ex) {
			System.err.println(ex);
		} finally {
			session.close();
		}
		return user;
	}

	public UserProfile getUserProfile(int id) {
		UserProfile user = null;

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		try {
			String hql = "Select u from UserProfile u where u.iduser=:iduser";
			Query query = session.createQuery(hql);
			query.setParameter("iduser", id);
			user = (UserProfile) query.uniqueResult();
		} catch (HibernateException ex) {
			System.err.println(ex);
		} finally {
			session.close();
		}
		return user;
	}

	public String getLasName(String email) {
		String lastName = "";

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		try {
			String hql = "Select lastName from UserProfile u where u.email=:email";
			Query query = session.createQuery(hql);
			query.setParameter("email", email);
			lastName = (String) query.uniqueResult();
		} catch (HibernateException ex) {
			System.err.println(ex);
		} finally {
			session.close();
		}
		return lastName;
	}

	// @Transactional
	public boolean addUserProfile(UserProfile userProfile, boolean enable) {

		if (getLasName(userProfile.getEmail()) != null)
			return false;

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			userProfile.getRoleses().add(RoleDAO.getRoles(1));
			userProfile.setEnable(enable);
			session.save(userProfile);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	public boolean addUserProfile(UserProfile userProfile, boolean enable, int roles) {

		if (getLasName(userProfile.getEmail()) != null)
			return false;

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			userProfile.getRoleses().add(RoleDAO.getRoles(roles));
			userProfile.setEnable(enable);
			session.save(userProfile);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	public boolean updateUserProfile(UserProfile userProfile, boolean enable, int roles) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			userProfile.setRoleses((RoleDAO.getRoles(roles)));
			userProfile.setEnable(enable);
			session.saveOrUpdate(userProfile);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	
	public boolean changeInfoUser(UserProfile userProfile)
	{
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			session.saveOrUpdate(userProfile);
			
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	public boolean deleteUserProfile(UserProfile userProfile){
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			session.delete(userProfile);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	
	public boolean isAdmin(String email) {
		String _email = "";

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		try {
			String hql = "Select u.email from UserProfile u inner join u.roleses ro where u.email=:email and ro.role=:role";
			Query query = session.createQuery(hql);
			query.setParameter("email", email);
			query.setParameter("role", "ROLE_ADMIN");
			_email = (String) query.uniqueResult();
		} catch (HibernateException ex) {
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		if (_email != "" && _email != null)
			return true;
		return false;
	}

	public List<UserProfile> getAllUser() {
		List<UserProfile> ds = null;

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		try {
			String hql = "Select u from UserProfile u";
			Query query = session.createQuery(hql);
			ds = query.list();
		} catch (HibernateException ex) {
			System.err.println(ex);
		} finally {
			session.close();
		}
		return ds;
	}
	
	public boolean changePasswordUser(String email, String oldPass, String newPass)
	{
		UserProfile userStore = getUserProfile(email);
		if (userStore == null || userStore.getPassword().equals(oldPass) == false)
		{
			return false;
		}
		userStore.setPassword(newPass);
		
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.update(userStore);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
}
